<?php
namespace adminmatrix\admin\validate;
use think\exception\ValidateException;
use think\Validate;

class BaseValidate extends Validate
{
    protected string $method = "GET";
    protected array $params  = [];
    public function post() : Validate
    {
        if ($this->request->isPost())
        {
            $this->params = $this->request->post();
            $this->method = "POST";
            return  $this;
        }
        throw new ValidateException('请求方式错误,请使用POST请求');
    }

    public function get() : Validate
    {
        if ($this->request->isGet())
        {
            $this->params = $this->request->param();
            return  $this;
        }
        throw new ValidateException("请求方式错误 , 请使用GET请求");
    }


    public function delete():Validate
    {
        if ($this->request->isDelete())
        {
            $this->params = $this->request->param();
            $this->method = "DELETE";
            return  $this;
        }
        throw new ValidateException("请求方式错误 , 请使用DELETE请求");
    }

    public function put() : Validate
    {
        if ($this->request->isPut())
        {
            $this->params = $this->request->param();
            $this->method = "PUT";
            return  $this;
        }
        throw new ValidateException("请求方式错误 ,请使用PUT请求");
    }


    public function goCheck( string $scene = '')
    {
        if ($scene)
        {
            $this->scene($scene);
        }
        $this->params = $this->request->param();
        if (!$this->check($this->params))
        {
            $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
            throw new ValidateException($exception);
        }
        return $this->params;
    }
}